<template>
  <div>
    <div class="nav">
      <!--返回-->
      <div class="goBack" @click="goback()" style="float: left">
        <van-icon name="revoke" />
      </div>
      <form action="/" style="overflow: hidden">
        <van-search
          v-model="value"
          shape="round"
          placeholder="搜索你感兴趣的新闻"
          @search="onSearch"
        />
      </form>
    </div>
    <div style="height: 54px"></div>
    <!--搜索历史-->
    <div class="searchHistory">
      <p>搜索历史</p>
      <ul class="listHistory">
        <li
          v-for="(item, index) in $store.state.historyList"
          :key="index"
          @click="find(index)"
        >
          {{ item }}
        </li>
      </ul>
    </div>
    <!--        搜索热榜-->
    <h3 v-show="flag">热门搜索</h3>
    <ul class="hotHistory" v-show="flag == true">
      <router-link
        :to="{ path: '/detail', query: { articleId: item.articleId } }"
        v-for="(item, index) in list"
        :key="index"
      >
        <li>{{ item.title }}</li>
      </router-link>
    </ul>
    <!-- 搜索到的新闻 -->
    <ul
      v-for="(item, index) in arr"
      :key="index"
      class="news"
      v-show="flag == false"
    >
      <router-link
        :to="{ path: '/detail', query: { articleId: item.articleId } }"
        tag="li"
      >
        <li>
          <img :src="item.bannerImgUrl" />
          <div class="textbox">
            <h5>{{ item.title }}</h5>
            <p>{{ item.articleSource }}</p>
            <p>{{ item.sendTime }}</p>
          </div>
        </li>
      </router-link>
    </ul>
  </div>
</template>
<script>
import { hotnews } from "../api/api";
// import storage from "good-storage";
export default {
  name: "Topnav",
  data() {
    return {
      value: "",
      list: [],
      //  搜索出来的数组
      arr: [],
      flag: true,
      //  历史数组
      historyList: [],
      historyList1:[]
    };
  },
  created() {
    hotnews(1, 5).then((res) => {
      this.list = res.rows;
    });
  },
  methods: {
    onSearch(val) {
      this.arr = []; //清空搜索数组
      var arr3 = this.$store.state.newList;
      for (var i = 0; i < arr3.length; i++) {
        //遍历vuex里的新闻数组
        if (arr3[i].title.includes(val)) {
          //新闻的title包含搜索值
          this.arr.push(arr3[i]);
        }
      }
      if (this.arr.length != 0) {
        //有搜索内容就显示
        this.flag = false;
      }
      this.$store.state.historyList.push(val);
    },
    //返回
    goback() {
      this.$router.go(-1);
    },
    find(index) {
      this.arr = [];
      var arr3 = this.$store.state.newList;
      for (var i = 0; i < arr3.length; i++) {
        if (arr3[i].title.includes(this.$store.state.historyList[index])) {
          this.arr.push(arr3[i]);
        }
      }
      if (this.arr.length != 0) {
        this.flag = false;
      }
    },
  },
};
</script>

<style lang="less" scoped>
.nav {
  overflow: hidden;
  position: fixed;
  top: 0;
  background-color: #409eff;
  width: 100%;
}
/deep/ .van-search {
  float: right;
  width: 100%;
  overflow: hidden;
}
.goBack {
  margin: 15px 30px 0px 15px;
  float: left;
  font-size: 23px;
}

.searchHistory p {
  margin-top: 20px;
  color: #b2b1b1;
  padding-left: 10px;
}
/*搜索历史*/
.listHistory {
  margin-top: 20px;
  overflow: hidden;
}

.listHistory li {
  float: left;
  width: 20%;
  height: 30px;
  text-align: center;
  line-height: 30px;
  border-radius: 100px;
  border: 1px solid #bbd9f1;
  margin: 5px;
}

/*搜索热榜*/
.hotHistory {
  margin-top: 10px;
  overflow: hidden;
}
.hotHistory li {
  float: left;
  width: 45%;
  height: 30px;
  text-align: center;
  line-height: 30px;
  margin: 8px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  letter-spacing: 1px;
  font-size: 15px;
  color: black;
}

h3 {
  margin-top: 100px;
  font-size: 18px;
  margin-left: 10px;
  color: #79beff;
}
// 搜索出来的新闻
.news {
  width: 100%;
  height: 100px;
  box-sizing: border-box;
  padding: 8px;
  border-bottom: 1px solid #ddd;
  li {
    width: 400px;
    height: 90px;
  }
  img {
    float: left;
    width: 38%;
    height: 100%;
  }
}
.textbox {
  width: 59%;
  height: 100%;
  float: left;
  margin-left: 7px;
  box-sizing: border-box;
  padding-left: 5px;
  h5 {
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
  }
  p {
    font-size: 12px;
    color: #999;
    word-break: break-all;
    margin-top: 12px;
  }
}
</style>